package com.xxkk.spring_boot_web;

import java.io.*;
import java.util.*;

/**
 * @author xxkk
 * @version 1.0.0
 * @title test
 * @description ʕ•ᴥ•ʔ
 * @create 2025/4/23 19:42
 **/
public class Test {

    Test() {

    }

    int w = 5;
    int h = 4;
    int[][] card_group = new int[h][w];

    public void generator_card_group() {
        Random rd = new Random();
        System.out.println("card:");
        int[][] cg = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cg[i][j] = rd.nextInt(5);
            }
            System.out.println(Arrays.toString(cg[i]));
        }
        this.card_group = cg;
    }

    public void t1() throws IOException, ClassNotFoundException {
        this.generator_card_group();
        List<LinkedList<Integer>> rewardLines = new LinkedList<>();
//        for (int i = 0; i < h; i++) {
//            LinkedList<Integer> card_ls = new LinkedList<>();
//            card_ls.add(i);
//            this.c_l(1, card_group[i][0], card_ls, rewardLines);
//        }
        this.c_l(rewardLines);

        System.out.println("line:");
        for (List ls : rewardLines
        ) {
            System.out.println(Arrays.toString(ls.toArray()));
        }
    }

    private void c_l(List<LinkedList<Integer>> rewardLines) throws IOException, ClassNotFoundException {
//        this.c_l0(wIndex, cardId, cardLs, rewardLines);
        for (int i = 0; i < h; i++) {
            LinkedList<Integer> card_ls = new LinkedList<>();
            card_ls.add(i);
            this.c_l0(1, card_group[i][0], card_ls, rewardLines);
        }
    }

    private void c_l0(int wIndex, int cardId, LinkedList<Integer> cardLs, List<LinkedList<Integer>> rewardLines) throws IOException, ClassNotFoundException {
        if (wIndex >= w) {
            rewardLines.add(cardLs);
            return;
        }
        boolean copy_flag = false;
        for (int i = 0; i < h; i++) {
            if (cardId == card_group[i][wIndex]) {
                LinkedList<Integer> ls = deepCopy(cardLs);
                ls.add(i);
                this.c_l0(wIndex + 1, cardId, ls, rewardLines);
                copy_flag = true;
            }
        }
        if (!copy_flag && wIndex >= 2) {
            rewardLines.add(cardLs);
        }
    }

    public static <T> T deepCopy(T object) throws ClassNotFoundException, IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(object);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (T) ois.readObject();
    }


    public static void main(String[] args) {
        Test test = new Test();
        try {
            test.t1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
